import { select,find,insert,edit,remove,parentSelect,getMenuList,setField} from '@/services/AdminMenu';
import {setRefault} from '@/utils/Base';
import { message } from 'antd';
const AdminMenuModel = {
  namespace: 'AdminMenu',
  state: {
    menuList:{//集体数据
        list: [],
        paginate:{},
    },
    refault:{},
    menuData:{},//个体数据
    ParentData:{},
    adminMenuList:[],//后台路由递归后的数组
  },
  effects: {
    /**
     * 
     * @param {获取后台路由数据} param0 
     * @param {*} param1 
     * 返回结构体为 递归后的三维数组
     */
    *getMenuList({ payload ,callback}, { call, put }) {
      const response = yield call(getMenuList,payload);
      yield put({
        type: 'saveMenuList',
        payload: response,
      });
      setRefault(response);
      (callback && typeof(callback) === "function") && callback(response);
    },
    *select({ payload }, { call, put }) {
    //console.log('rrrrrrrrrrrr');
      const response = yield call(select,payload);
      yield put({
        type: 'saveSelect',
        payload: response,
      });
      setRefault(response);
    },
    *parentSelect({ payload,callback }, { call, put }) {
      const response = yield call(parentSelect,payload);
      yield put({
        type: 'saveParentSelect',
        payload: response,
      });
      setRefault(response);
      (callback && typeof(callback) === "function") && callback(response);
    },
    *insert({ payload,callback }, { call, put }) {
        //console.log('params',payload);
      const response = yield call(insert,payload);
      yield put({
        type: 'saveInsert',
        payload: response,
      });
      setRefault(response);
      (callback && typeof(callback) === "function") && callback(response);
    },
    *edit({ payload,callback }, { call, put }) {
        //console.log('params',payload);
      const response = yield call(edit,payload);
      yield put({
        type: 'saveInsert',
        payload: response,
      });
      setRefault(response);
      (callback && typeof(callback) === "function") && callback(response);
    },
    *setField({ payload,callback }, { call, put }) {
        //console.log('params',payload);
      const response = yield call(setField,payload);
      yield put({
        type: 'saveSetField',
        payload: response,
      });
      setRefault(response);
      (callback && typeof(callback) === "function") && callback(response);
    },
    *remove({ payload,callback }, { call, put }) {
      //console.log('params',payload);
      const response = yield call(remove,payload);
      yield put({
        type: 'saveRemove',
        payload: response,
      });
      setRefault(response);
      (callback && typeof(callback) === "function") && callback(response);
    },
    *find({ payload,callback }, { call, put }) {
      const response = yield call( find , payload );
      yield put({
        type:'saveFind',
        payload:response,
      });
      setRefault(response);
      (callback && typeof(callback) === "function") && callback(response);
    },
    *setDataIcon({ payload }, {  put }){
      yield put({
        type:'saveDataIcon',
        payload:payload,
      });
    },
  },
  reducers: {
    saveMenuList(state,action){
      return {
        ...state,
        adminMenuList:action.payload.data.list
      }
    },
    saveDataIcon(state,action){
      const {payload} = action;
      const {menuData} = state;
      menuData.icon = payload.icon;
      return {
        ...state,
        menuData:menuData
      }
    },
    saveSelect(state, action) {
      var data = {};
      if(action.payload.code === 1){
          data = action.payload.data;
      }else{
          message.error(action.payload.msg);
      }
      return {
        ...state,
        menuList: data,//这个地方回调执行更新数据格式一个大坑 必须写data才可以
      };
    },
    /**
     * 保存父级路由
     */
    saveParentSelect( state , {payload} ){
      //console.log('action',payload.data);
      return { ...state,ParentData:payload.data};
    },
    saveRemove( state , {payload} ){
      console.log('action',payload);
      return { ...state,refault:payload};
    },
    saveFind( state , {payload} ){
      //console.log('action',payload);
      return { ...state,menuData:payload.data};
    },
    saveSetField(state,action){
        if(action.payload.code === 1){
            message.success(action.payload.msg);
        }else{
            message.error(action.payload.msg);
        }
        return { ...state,refault:action.payload};
    },
    saveInsert(state,action){
        if(action.payload.code === 1){
            message.success(action.payload.msg);
        }else{
            message.error(action.payload.msg);
        }
        return { ...state,refault:action.payload};
    },
    changeNotifyCount(
      state = {
        currentUser: {},
      },
      action,
    ) {
      return {
        ...state,
        currentUser: {
          ...state.currentUser,
          list:list
        },
      };
    },
  },
};
export default AdminMenuModel;


